package com.sitech.openapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.domain.AsyncFeedBack;
import com.sitech.openapi.domain.Test;
import com.sitech.openapi.service.ProdPreOrderService;
import com.sitech.openapi.service.ProductOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangsq on 2018/12/5.
 * 关于随选宽带甩单集约销售支撑工作的通知需求
 * 修改产品订单：
 */
@RestController
@Api(tags = "宽带随选修改产品订单", description = "宽带随选修改产品订单控制层")
@Slf4j
public class ProductOrderController {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;
    @Autowired
    ProductOrderService productOrderService;

    @PatchMapping("/productOrder/{custOrderId}")
    @ApiOperation(value = "修改产品订单", notes = "宽带随选修改产品订单")
    public ResponseEntity<Map<String, Object>> productOrderBack(@PathVariable String custOrderId,
                                                                @RequestHeader HttpHeaders headers) throws Exception {

        log.info("入参<><><><>custOrderId=" + custOrderId + "<><><><>=headers" + headers);
        Map inParam = new HashMap();
        inParam.put("custOrderId", custOrderId);
        inParam.put("staffCode", headers.get("Staff_Code"));
        Map retMap = productOrderService.updateProductOrder(inParam);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<Map<String, Object>> res = new ResponseEntity<Map<String, Object>>(retMap, httpHeaders, HttpStatus.OK);
        return res;
    }

    @ExceptionHandler
    public ResponseEntity<Map<String, String>> productOrderError(Exception e, NativeWebRequest request) {
        Map<String, String> errors = new HashMap<>();
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message", e.toString());
        errors.put("referenceError", "");

        HttpHeaders httpHeaders = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }
}
